package com.qfedu.edu.seckill.handler;

import com.qfedu.edu.seckill.manager.RedisManager;
import com.qfedu.edu.seckill.result.R;
import com.qfedu.edu.seckill.utils.HttpResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 这个就是退出的时候你要进行的操作
 */
@Component
@Slf4j
public class MyLogoutHandler implements LogoutHandler {

    @Autowired
    private RedisManager redisManager;

    @Override
    public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        //拿到这个token  去redis中删除数据  告诉客户端退出了...
        String token = request.getHeader("token");
        //去Redis中删除数据
        redisManager.deleteKey(token);
        log.info("退出成功...");
        //告诉这个客户端这个退出成功
        HttpResponseUtils.sendResponse(response,R.ok());
    }
}
